/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.redteam.models;

import com.redteam.data.beans.Person;
import com.redteam.data.dao.PersonsDAO;
import com.redteam.data.dao.interfaces.DAO;
import com.redteam.util.DateConverter;
import java.sql.Date;
import java.sql.SQLException;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Used to insert new people into the database.
 * @author Jeremy
 */
public class PersonModel {

    /**
     * Default constructor
     */
    public PersonModel() {
    }

    /**
     * Inserts a new person
     * @param newperson
     * @return
     */
    public int insertPerson(Map<String, String[]> newperson) {
        DAO dao = PersonsDAO.getInstance();
        try {
            Person person = new Person();
            person.setfBOId(Long.parseLong(newperson.get("fBOId")[0]));
            person.setFirstName(newperson.get("firstName")[0]);
            person.setMiddleName(newperson.get("middleName")[0]);
            person.setLastName(newperson.get("lastName")[0]);
            person.setStreet(newperson.get("street")[0]);
            person.setCity(newperson.get("city")[0]);
            person.setState(newperson.get("state")[0]);
            person.setZipCode(newperson.get("zipCode")[0]);
            person.setEmail(newperson.get("email")[0]);
            person.setBirthDate(Date.valueOf(DateConverter.convertDate(newperson.get("birthdate")[0])));
            person.setHomePhone(newperson.get("homePhone")[0]);
            person.setCellPhone(newperson.get("cellPhone")[0]);
            person.setWorkPhone(newperson.get("workPhone")[0]);
            person.setGender(newperson.get("gender")[0]);
            person.setDiscriminator(newperson.get("discriminator")[0]);
            person.setUsername(newperson.get("username")[0]);
            person.setPassword(newperson.get("password")[0]);
            return dao.insert(person);
        } catch (SQLException ex) {
            Logger.getLogger(DAOAccessor.class.getName()).log(Level.SEVERE, null, ex);
        }
        return 0;
        //TODO: planes for student: one endorsement up
    }
}
